#include "printplugin.h"
#include <QtPlugin>
#include <QApplication>
#include <QAction>
#include <QProgressDialog>
#include <QPrintPreviewDialog>
#include <QPainter>
#include "printwindow.h"

Q_EXPORT_PLUGIN2(printplugin, PrintPlugin)

PrintPlugin::PrintPlugin(QObject * parent)
    : QObject(parent)
{
}

PrintPlugin::~PrintPlugin()
{
}

void PrintPlugin::setup(PioCore * core)
{
    m_core = core;

    m_window = new PrintWindow(m_core, m_core->mainwindow());
    m_window->hide();
    m_action = new QAction(tr("Print selection..."), this);
    m_action->setShortcut(QKeySequence::Print);
    m_core->toolbar()->addAction(m_action);

    connect(m_action, SIGNAL(triggered()), this, SLOT(print()));
    //connect(m_core, SIGNAL(selectionChanged(QList<MediaItemPtr>)), m_window->engine(), SLOT(setSelection(QList<MediaItemPtr>)));
}

QStringList PrintPlugin::licenseInfo()
{
    return QStringList();
}

void PrintPlugin::print()
{
/*    QPrintPreviewDialog d;
    connect(&d, SIGNAL(paintRequested(QPrinter*)), this, SLOT(print(QPrinter*)));
    d.exec();*/

    if(m_window->isVisible())
        m_window->raise();
    else {
        m_window->engine()->setSelection(m_core->selection());
        m_window->show();
    }
}

void PrintPlugin::print(QPrinter * printer)
{
    QPainter painter;
    painter.begin(printer);
    painter.drawText(100, 100, "page 1");
    printer->newPage();
    painter.drawText(100, 100, "page 2");
    painter.end();
}
